package com.hopes.bizz.risk.transfer.handler;

import com.hopes.bizz.risk.control.entity.RiskControlMeasureEntity;
import com.hopes.bizz.risk.control.entity.RiskMeasureTaskConfigEntity;
import com.hopes.bizz.risk.control.entity.RiskMeasureTaskEntity;
import com.hopes.bizz.risk.control.service.RiskControlMeasureService;
import com.hopes.bizz.risk.control.service.RiskMeasureTaskConfigService;
import com.hopes.bizz.risk.control.service.RiskMeasureTaskService;
import com.hopes.bizz.risk.transfer.bean.bo.RiskDataBo;
import com.hopes.bizz.risk.transfer.bean.dto.RiskMeasuresTaskDto;
import com.hopes.bizz.risk.transfer.constant.RiskErrorType;
import com.hopes.bizz.risk.transfer.constant.RiskRequestType;
import com.hopes.bizz.risk.transfer.converter.RiskMeasuresTaskConverter;
import com.hopes.boss.common.core.constant.CommonConstants;
import com.hopes.boss.common.data.org.OrgContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author lyb
 * @version 1.0
 * @date 2024/2/4 14:11
 */
@Service
@Slf4j
public class RiskMeasuresTaskHandler extends AbstractRiskData<RiskMeasuresTaskDto> {

    @Resource
    private RiskControlMeasureService riskControlMeasureService;

    @Resource
    private RiskMeasureTaskService riskMeasureTaskService;

    @Resource
    private RiskMeasureTaskConfigService riskMeasureTaskConfigService;

    @Override
    protected void saveRiskData(RiskMeasuresTaskDto riskMeasuresTaskDto, RiskDataBo riskDataBo) {
        // 判断关联管控措施是否存在
        String riskMeasureId = riskMeasuresTaskDto.getRiskMeasureId();
        RiskControlMeasureEntity riskControlMeasure = riskControlMeasureService.getByUid(riskMeasureId);
        if (Objects.isNull(riskControlMeasure) || CommonConstants.STATUS_DEL.equals(riskControlMeasure.getDelFlag())) {
            saveErrorLog(riskMeasuresTaskDto, riskDataBo, RiskErrorType.RISK_MEASURE_NOT_FOUND);
            return;
        }
        String uid = riskMeasuresTaskDto.getId();
        RiskMeasureTaskEntity riskMeasureTask = riskMeasureTaskService.getByUid(uid);
        RiskMeasureTaskEntity riskMeasureTaskEntity = RiskMeasuresTaskConverter.INSTANCE.toEntity(riskMeasuresTaskDto);
        riskMeasureTaskEntity.setRiskUnitId(riskControlMeasure.getRiskUnitId());
        riskMeasureTaskEntity.setRiskEventId(riskControlMeasure.getRiskEventId());
        riskMeasureTaskEntity.setRiskControlId(riskControlMeasure.getId());
        riskMeasureTaskEntity.setOrgId(riskDataBo.getOrgId());
        if (Objects.isNull(riskMeasureTask)) {
            // 保存隐患排查内容配置
            RiskMeasureTaskConfigEntity taskConfig = new RiskMeasureTaskConfigEntity();
            taskConfig.setRiskUnitId(riskControlMeasure.getRiskUnitId());
            taskConfig.setRiskEventId(riskControlMeasure.getRiskEventId());
            taskConfig.setRiskControlId(riskControlMeasure.getId());
            taskConfig.setCheckContent(riskMeasureTaskEntity.getCheckContent());
            taskConfig.setOrgId(riskDataBo.getOrgId());
            riskMeasureTaskConfigService.save(taskConfig);
            // 保存
            riskMeasureTaskEntity.setRiskTaskConfigId(taskConfig.getId());
            riskMeasureTaskService.save(riskMeasureTaskEntity);

        } else {
            // 更新
            riskMeasureTaskEntity.setId(riskMeasureTask.getId());
            riskMeasureTaskService.updateById(riskMeasureTaskEntity);
            // 更新隐患排查内容配置
            riskMeasureTaskConfigService.lambdaUpdate()
                    .set(RiskMeasureTaskConfigEntity::getRiskUnitId, riskControlMeasure.getRiskUnitId())
                    .set(RiskMeasureTaskConfigEntity::getRiskEventId, riskControlMeasure.getRiskEventId())
                    .set(RiskMeasureTaskConfigEntity::getCheckContent, riskMeasureTaskEntity.getCheckContent())
                    .eq(RiskMeasureTaskConfigEntity::getId, riskMeasureTask.getRiskTaskConfigId());
        }
    }

    @Override
    public RiskRequestType getRequestType() {
        return RiskRequestType.RISK_MEASURES_TASK;
    }
}
